﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using CanDoo;
using CanDoo.Web;
using CanDoo.Data;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Admin.Core.Models;
using CanDoo.Logging;
using CanDoo.Admin.Core.Identity;
using CanDoo.Core.Security;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity;

namespace CanDoo.Oil.WebHost
{
    public partial class Default : CanDoo.FineUI.PageBase
    {
        public ISiteConfig SiteConfig { get; set; }

        public UserManager UserManager { get; set; }
        public SignInManager SignInManager { get; set; }
        public IBulletinContract BulletinContract { get; set; }

        public string BulletinStr = "";
        //public IBulletinContract _iBulletinContract { get; set; }
        //public IUserContract _iUserContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var username = "candoo";
                var user = UserManager.FindByName(username);
                if(user == null)
                {
                    user = new User() { NickName = "candoo", Name = "系统开发员", UserName = "candoo",CreatedTime=DateTime.Now,Enable=true};
                    var rtn = UserManager.Create(user, "candoo");
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Password_txt.Attributes.Add("value", Password_txt.Text);
            Code_txt.Text = "";
        }

        protected void Login_Btn_Click(object sender, EventArgs e)
        {
            string Account = Account_txt.Text.Trim().Replace("'", "");
            string Password = Password_txt.Text.Trim().Replace("'", "");
            string Code = Code_txt.Text.Trim();

            HttpCookie cookie1 = new HttpCookie("Account");
            cookie1.Value = Account;
            cookie1.Expires = DateTime.Now.AddDays(365);
            Response.Cookies.Add(cookie1);

            if (Account == "")
            {
                Label1.Text = "<span style='color:red'>登录失败：请输入登陆帐号！</span>";
                return;
            }
            if (Password == "")
            {
                Label1.Text = "<span style='color:red'>登录失败：请输入登陆密码！</span>";
                return;
            }
            //if (Code.Length != 4)
            //{
            //    Label1.Text = "<span style='color:red'>登录失败：请输入4位验证码！</span>";
            //    return;
            //}

            //if (Web.Session.Read<string>("CheckCode") == null || Code.ToLower() != Web.Session.Read<string>("CheckCode").ToLower())
            //{
            //    Label1.Text = "<span style='color:red'>登录失败：验证码过期或错误！</span>";
            //    return;
            //}

            // 这不会计入到为执行帐户锁定而统计的登录失败次数中
            // 若要在多次输入错误密码的情况下触发锁定，请更改为 shouldLockout: true
            var result = SignInManager.PasswordSignIn(Account, Password, RememberMe.Checked, shouldLockout: false);

            switch (result)
            {
                case SignInStatus.Success:
                    PermissionManager.ClearCache(Account);//清除用户权限缓存
                    //IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    Response.Redirect("~/Admin/");
                    break;
                case SignInStatus.LockedOut:
                    Response.Redirect("/Account/Lockout");
                    break;
                case SignInStatus.RequiresVerification:
                    Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
                                                    Request.QueryString["ReturnUrl"],
                                                    RememberMe.Checked),
                                      true);
                    break;
                case SignInStatus.Failure:
                default:
                    Label1.Text = "无效的登录尝试";
                    Label1.Visible = true;
                    break;
            }
            
        }
    }
}